package com.incar.chuxing.utils.Dialogs;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.incar.chuxing.R;
import com.incar.chuxing.adapter.UserHelpAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by user1 on 2017/6/29.
 */

public class UserHelpDialog extends Dialog {
    private String[] title;
    private String[] content;
    private int[] imageIds;
    private List<View> viewList = new ArrayList<>();

    private RadioButton[] arrRadioButton = null;

    private ImageView help_close;
    private ViewPager viewPager_guide;
    private RadioGroup radioGroup_guide;
    private UserHelpAdapter adapter = null;

    public UserHelpDialog(Context context) {
        super(context, R.style.Loading_Dialog_Dim);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        setCancelable(true);
        setCanceledOnTouchOutside(false);
        setContentView(R.layout.activity_help);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(params);
        initData();
        initView();
    }

    private void initData() {
        title = new String[]{"如何预约", "如何开门", "如何还车", "停车区域"};
        content = new String[]{"在地图上点击立即用车即可预订车辆，超出预留时间仍未开门用车，订单自动取消。",
                "点击开门按钮，车辆车锁打开，开锁后自动取消。",
                "车辆使用完成后，需将车辆停在围栏内还车，然后点击结束行程去结算，再次确认还车，即可结束计费。",
                "用车范围内的公共还车区。若停到指定还车区内，会收取额外的调度费。"};
        imageIds = new int[]{R.drawable.pic_shiyongzhinan1, R.drawable.pic_shiyongzhinan2, R.drawable.pic_shiyongzhinan3, R.drawable.pic_shiyongzhinan4};
    }

    private void initView() {
        viewPager_guide = (ViewPager) findViewById(R.id.vp_help);
        help_close = (ImageView) findViewById(R.id.help_close);
        radioGroup_guide = (RadioGroup) findViewById(R.id.radio_help);
        for (int i = 0; i < 4; i++) {
            View view = View.inflate(getContext(), R.layout.item_user_help, null);
            ((TextView) (view.findViewById(R.id.help_title))).setText(title[i]);
            ((TextView) (view.findViewById(R.id.help_content))).setText(content[i]);
            ((ImageView) (view.findViewById(R.id.help_image))).setImageDrawable(getContext().getResources().getDrawable(imageIds[i]));
            viewList.add(view);
        }
        help_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        adapter = new UserHelpAdapter(viewList);
        viewPager_guide.setAdapter(adapter);
        initDots();
        viewPager_guide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                arrRadioButton[position].setChecked(true);
                if (position == (adapter.getCount() - 1)) {

                } else {

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }


    private void initDots() {
        arrRadioButton = new RadioButton[viewList.size()];
        for (int i = 0; i < viewList.size(); i++) {
            RadioButton radioButton = new RadioButton(getContext());
            radioButton.setButtonDrawable(R.drawable.dots);
            radioButton.setPadding(15, 0, 15, 0);
            radioGroup_guide.addView(radioButton);
            arrRadioButton[i] = radioButton;
        }
        //让radioButton第一个被勾选
        arrRadioButton[0].setChecked(true);
        radioGroup_guide.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < arrRadioButton.length; i++) {
                    if (arrRadioButton[i].getId() == checkedId) {
                        viewPager_guide.setCurrentItem(i);
                    }
                }
            }
        });
    }


    @Override
    public void show() {//在要用到的地方调用这个方法
        super.show();
    }


    @Override
    public void dismiss() {
        super.dismiss();
    }


}